package com.mm.controller;

import com.mm.domain.user.DUser;
import com.mm.serivce.user.IDPermissionService;
import com.mm.util.common.RedisUtil;
import com.mm.util.common.SessionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 主页
 *
 * @author: yanglin
 * @Date: 2021-08-04-14:11
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("")
public class IndexController {

    private final RedisUtil redisUtil;
    public IndexController(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout() {
        ModelAndView modelAndView = new ModelAndView("login");
        HttpSession session = SessionUtil.getSession();
        DUser user = (DUser) session.getAttribute("user");
        String token = (String) session.getAttribute("token");
        if (Objects.nonNull(user)) {
            String userKey = "user:" + token;
            redisUtil.del(userKey);
        }
        session.removeAttribute("user");
        session.removeAttribute("menuList");
        return modelAndView;
    }

    /**
     * 主页
     *
     * @return
     */
    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("home");
        DUser user = (DUser) request.getSession().getAttribute("user");
        if (Objects.nonNull(user)) {
            //List<DPermission> permissions = permissionService.queryMenuByUser(user.getUserId());
            // 用户信息
            modelAndView.addObject("loginUser", user);
            // 用户菜单
            //modelAndView.addObject("menuList", permissions);
        } else {
            modelAndView = new ModelAndView("redirect:/logout");
        }
        return modelAndView;
    }

    /**
     * 等级
     *
     * @return
     */
    @RequestMapping("/level")
    public ModelAndView level() {
        ModelAndView modelAndView = new ModelAndView("mumu/level/level");
        return modelAndView;
    }

    /**
     * 用户
     *
     * @return
     */
    @RequestMapping("/user")
    public ModelAndView user(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("mumu/user/user");
        DUser user = (DUser) request.getSession().getAttribute("user");
        if (Objects.nonNull(user)) {
            // 用户信息
            if (user.getRoles().size() > 0) {
                List<String> roleIds = user.getRoles().stream().map(r -> String.valueOf(r.getRoleId()))
                        .collect(Collectors.toList());
                String roleIdStr = roleIds.stream().collect(Collectors.joining(","));
                modelAndView.addObject("roleIdStr", roleIdStr);
            } else {
                modelAndView.addObject("roleIdStr", "");
            }
        }
        return modelAndView;
    }

    /**
     * 角色
     *
     * @return
     */
    @RequestMapping("/role")
    public ModelAndView role() {
        ModelAndView modelAndView = new ModelAndView("mumu/role/role");
        return modelAndView;
    }

    /**
     * 权限菜单
     *
     * @return
     */
    @RequestMapping("/permission")
    public ModelAndView permission() {
        ModelAndView modelAndView = new ModelAndView("mumu/permission/permission");
        return modelAndView;
    }

    /**
     * 司机
     *
     * @return
     */
    @RequestMapping("/motorman")
    public ModelAndView motorman() {
        ModelAndView modelAndView = new ModelAndView("mumu/motorman/motorman");
        return modelAndView;
    }

    /**
     * 乘客
     *
     * @return
     */
    @RequestMapping("/passenger")
    public ModelAndView passenger() {
        ModelAndView modelAndView = new ModelAndView("mumu/passenger/passenger");
        return modelAndView;
    }

    /**
     * 车辆
     *
     * @return
     */
    @RequestMapping("/car")
    public ModelAndView car() {
        ModelAndView modelAndView = new ModelAndView("mumu/car/car");
        return modelAndView;
    }

    /**
     * 订单
     *
     * @return
     */
    @RequestMapping("/order")
    public ModelAndView order() {
        ModelAndView modelAndView = new ModelAndView("mumu/order/order");
        return modelAndView;
    }

    /**
     * 地址
     *
     * @return
     */
    @RequestMapping("/address")
    public ModelAndView address() {
        ModelAndView modelAndView = new ModelAndView("mumu/address/address");
        return modelAndView;
    }

    /**
     * 公共配置
     *
     * @return
     */
    @RequestMapping("/commoncode")
    public ModelAndView commonCode() {
        ModelAndView modelAndView = new ModelAndView("mumu/commoncode/commoncode");
        return modelAndView;
    }

    /**
     * 小说网站爬取展示
     *
     * @return
     */
    @RequestMapping("/booksource")
    public ModelAndView bookSource() {
        ModelAndView modelAndView = new ModelAndView("book/booksource/booksource");
        return modelAndView;
    }

    /**
     * 小说列表展示下载存储
     *
     * @return
     */
    @RequestMapping("/bookinfo")
    public ModelAndView bookinfo() {
        ModelAndView modelAndView = new ModelAndView("book/bookinfo/bookinfo");
        return modelAndView;
    }

    @RequestMapping("/note")
    public ModelAndView note() {
        ModelAndView modelAndView = new ModelAndView("note/note");
        return modelAndView;
    }


    @RequestMapping("/edit")
    public ModelAndView noteEdit(@RequestParam String nId, @RequestParam String text) {
        log.info("进入编辑.............{}", nId);
        ModelAndView modelAndView = new ModelAndView("note/edit");
        modelAndView.addObject("nId", nId);
        modelAndView.addObject("text", text);
        return modelAndView;
    }

    @RequestMapping("/edit1")
    public ModelAndView noteEdit1(@RequestParam String nId, @RequestParam String text, @RequestParam String createTime) {
        log.info("进入编辑1.............{}", nId);
        ModelAndView modelAndView = new ModelAndView("note/edit1");
        modelAndView.addObject("nId", nId);
        modelAndView.addObject("text", text);
        modelAndView.addObject("createTime", createTime);
        return modelAndView;
    }

    @RequestMapping("/export")
    public ModelAndView exportNote() {
        ModelAndView modelAndView = new ModelAndView("note/export");
        return modelAndView;
    }

    @RequestMapping("/task")
    public ModelAndView task() {
        ModelAndView modelAndView = new ModelAndView("note/task");
        return modelAndView;
    }
}
